package com.gulimall.seckill.scheduled;

import com.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author kkk
 * @date 2022/8/8
 */
@Component
@Slf4j
public class SeckillSkuScheduled {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;
    private static final String LOCK_NAME = "seckill:update:lock";

    //    @Scheduled(cron = "0 0 3 * * ?")
    @Scheduled(cron = "*/3 * * * * ?")
    public void updateSeckillSkuLast3Days() {
        log.info("开始执行");
        RLock lock = redissonClient.getLock(LOCK_NAME);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.updateSeckillSkuLast3Days();
        } finally {
            lock.unlock();
        }
    }
}
